home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / 13h_kit / images.hpp < prev    next >
C/C++ Source or Header  |  1991-07-04  |  13KB  |  283 lines

  1. //*************************************************************************
  2. // IMAGES.HPP -Header file for basic graphics operations and PCX          *
  3. //               image decoding in graphics mode 13h                      *
  4. //                                                                            *
  5. // Copyright 1991 by the Gamers Programming Workshop, a function of the   *
  6. // GAMERS forum, Compuserve. For more info e-mail 76605,2346.             *
  7. //                                                                        *
  8. // License is granted for use or modification of this code as long as     *
  9. // this notice remains intact, and all improvements are listed in the     *
  10. // version history below, and uploaded to the GAMERS forum. This code     *
  11. // may not be used for any commercial purpose.                            *
  12. //*************************************************************************
  13.  
  14. //*************************************************************************
  15. // Version history:                                                       *
  16. //                                                                        *
  17. // Version 1.0                                                            *
  18. // Developed: May 2, 1991                                                 *
  19. // Author:    Mark Betz, 76605,2346                                       *
  20. // Last update: July 5, 1991                                              *
  21. //*************************************************************************
  22.  
  23. // ************************************************************************
  24. // The functions and class structures declared in this header provide some
  25. // basic tools for 256 color mode 13h. Tools are provided for initializing
  26. // a pcx graphic file, unpacking PCX encoded data, setting text and graphics
  27. // modes, loading the dac palette, reading the dac palette, writing a pixel
  28. // reading a pixel, bar fills, and fast line drawing. In addition, the class
  29. // font provides methods for displaying and reading strings in a bitmapped
  30. // font. See images.doc for info on images tools, and font.doc for info on
  31. // using the font class.
  32. // ************************************************************************
  33.  
  34. // ************************************************************************
  35. // this is the 128 byte header at the front of all PCX files. In 256 color
  36. // mode 0x13 the palette structure is ignored, and the palette can be found
  37. // in the last 768 bytes of the file.
  38. // ************************************************************************
  39.  
  40. struct pcx_hdr {
  41.     char Mfg;                // manufacturer, always 0xa0
  42.     char Ver;                // encoder version number
  43.     char Enc;                // encoding code, always 1
  44.     char Bpp;                // bits per pixel, 8 in mode 0x13
  45.     int     Xmin,Ymin;            // image origin, usually 0,0
  46.     int     Xmax,Ymax;            // image dimensions
  47.     int     Hres;                // horizontal resolution value
  48.     int     Vres;                // vertical resolution value
  49.     char Pal[48];            // palette (not in mode 0x13)
  50.     char Reserved;           // who knows?
  51.     char ClrPlanes;            // number of planes, 1 in mode 0x13
  52.     int     Bpl;                // bytes per line, 80 in mode 0x13
  53.     int     PalType;            // Grey or Color palette flag
  54.     char Filler[58];        // Zsoft wanted a 128 byte header
  55. };
  56.  
  57. // **************************************************************************
  58. // p_rec is the primary rgb dac palette structure type
  59. // **************************************************************************
  60.  
  61. typedef char p_rec[256][3];         // basic rgb palette structure type
  62.  
  63. // **************************************************************************
  64. // this enum is used to specify in several places how the background will be
  65. // treated.
  66. // **************************************************************************
  67.  
  68. enum opacity {trans,opaque};
  69.  
  70. // **************************************************************************
  71. // declaration of font class (see font.doc). All methods define in images.cpp
  72. // **************************************************************************
  73.  
  74. class font {                              // generic font class
  75.     public:
  76.     opacity see_thru;                     // background treatment
  77.  
  78.     font(char *f_spec);                   // constructor, pass file name
  79.     void setstyle(int f_grnd, int b_grnd,
  80.                   short char_tab, opacity o,
  81.                   char space_wid);
  82.     int getforecolor();                   // get current text color
  83.     int getbackcolor();                   // get current background color
  84.     void show(int x, int y,char *str);    // show a string at x,y
  85.     void readstr(int x, int y,char *str,
  86.                  char length, char mask); // read a string from keyboard
  87.     int installed();                      // returns 1 if font installed
  88.     ~font();                              // destructor frees font memory
  89.  
  90.     private:
  91.     struct font_hdr {              // font description structure
  92.         char  font_name[8];        // font name, no file extension
  93.         char  fram_wid;            // width of character frame in pixels
  94.         char  fram_hit;            // height of character frame in pixels
  95.         char  base_ofs;            // baseline y offset from top
  96.         char  ascii_first;         // number of first character defined
  97.         char  ascii_last;          // number of last character defined
  98.         char f_color;              // original font foreground color
  99.     } f_hdr;
  100.     char width[128];               // character width table
  101.     char spacing;                  // pixels between characters
  102.     char far *font_ptr;            // pointer to font in memory
  103.     char far *cursor_mask;         // pointer to cursor mask in memory
  104.     int fore_color;                // holds current text color
  105.     int back_color;                // holds current background color
  106.     int status;                    // non 0 if installation successful
  107.  
  108.     void put_char(int x, int y,    // display a single character
  109.                   char c);
  110.     char get_width(char c);
  111. };
  112.  
  113. // *************************************************************************
  114. // The i_stack class was originally envisioned as a pointer stack for the
  115. // use of a window management class that would create cascaded windows by
  116. // allocating pointers to win class objects and pushing them onto the
  117. // pointer stack. A close() method would close the last opened window. For
  118. // now this is on the back burner. If you see a use for the pointer stack
  119. // be aware that I haven't tested it at all. Should work <g>.
  120. // *************************************************************************
  121.  
  122.  
  123. class i_stack {
  124.     public:
  125.     i_stack();                     // constructor
  126.     char push_ptr(void far *ptr);  // push a pointer on to stack
  127.     void *pop_ptr();               // pop a pointer from the stack
  128.  
  129.     private:
  130.     void far *ptr_array[20];       // pointer container
  131.     char ptr_index;                // index to next open element
  132. };
  133.  
  134. // *************************************************************************
  135. // Class win is a pop up window class. A win object allocates memory for,
  136. // and saves it's own background. So consecutive windows can be opened, and
  137. // each will restore the background as it goes out of scope, or as it is
  138. // deleted(), depending on how it was opened. See WIN.DOC for a brief des-
  139. // cription.
  140. // *************************************************************************
  141.  
  142. class win {
  143.     public:
  144.     char status;             // 1 if window opened successfully
  145.  
  146.     win(int tlx, int tly, int xwid, int ywid, char *bmap,
  147.            char brdrwid, char shdwwid, char bgrnd, char border);
  148.  
  149.     char installed(){return(status);}   // returns the status value
  150.     ~win();
  151.  
  152.     private:
  153.     char far *backgrnd;      // pointer to background bitmap
  154.     int origX;               // top left x coord of the window
  155.     int origY;               // top left y coord of the window
  156.     int xsize;               // window x dimension in pixels
  157.     int ysize;               // window y dimension in pixels
  158.     char bdrwid;             // border width, 0 if no border
  159.     char shdwid;             // shadow width, 0 if no shadow
  160.     char shdwon;             // 1 if shadow is on, 0 if not
  161.     char b_grnd;             // window background color
  162.     char brdr;               // border color, ignored if bdrwid=0;
  163. };
  164.  
  165.  
  166. class image {
  167.     public:
  168.     p_rec palette;                        // holds the default image palette
  169.     char fpath[80];                       // stores the disk path to image
  170.     char getstatus() {return(status);}    // returns status of last op
  171.                                           // 1 if successful, 0 if not
  172.     protected:
  173.     char in_ram;                          // 1 if image in ram, 0 if not
  174.     char packed;                          // 1 if packed in ram, 0 if not
  175.     char status;                          // returned by getstatus()
  176.     char far *buffer;                     // pointer for ram buffer if any
  177. };
  178.  
  179. class pcx : public image {
  180.     public:
  181.     pcx(char *fspec);                     // constructor
  182.     char load(char method);               // load into ram packed or unpacked
  183.     void unload();                        // unload from ram
  184.     char display(char fadetype);          // display image using fadetype
  185.     void remove(char fadetype);           // remove image using fadetype
  186.     ~pcx();                                  // destructor
  187.     private:
  188.     char unpacktoram(char far *dest,      // internal, unpack to memory
  189.                      unsigned numbytes);
  190.  
  191. };
  192.  
  193. // image fade constants ***************************************************
  194.  
  195. const char SnapWipe = 0;             // simple block copy to vram
  196. const char SplitVerticalWipe = 1;    // fades from top and bottom
  197. const char SplitHorizWipe = 2;       // fades from right and left
  198. const char SlideVerticalWipe = 3;    // slides image in from right and left
  199. const char SlideHorizWipe = 4;       // slides image in from top and bottom
  200. const char VerticalDissolve = 5;     // fades multiple vertical slices
  201. const char HorizDissolve = 6;        // fades multiple horizontal slices
  202. const char SparkleDissolve = 7;      // fades with random pixel placement
  203. const char SoftFade=8;                 // does a palette shifted fade
  204.  
  205. // error reporting constants **********************************************
  206.  
  207. const char NoErr = 0;            // no error occured
  208. const char MemErr = 1;           // error occured allocating memory
  209. const char FileReadErr = 2;      // error occured reading a file
  210. const char FileWriteErr = 3;     // error occured writing a file
  211. const char FileMakeErr = 4;      // error occured creating a file
  212. const char FileOpenErr = 5;      // error occured opening file
  213. const char FileFormatErr = 6;    // bad file format error
  214. const char SecondaryErr = 7;     // error occured in subroutine during call
  215. const char UnknownErr = 8;       // an unknown error occured
  216.  
  217. // For use with type PRec *************************************************
  218.  
  219. const short Red = 0;               // constants used with type PRec
  220. const short Green = 1;             // example: PRec Palette;
  221. const short Blue = 2;               //          Palette[0][Green]=63;
  222.  
  223. // for use with call to pcx::load() ***************************************
  224.  
  225. const char Packed = 0;             // load image in compressed form
  226. const char Unpacked = 1;           // load image in uncompressed form
  227. const char Bestfit = 2;            // load compressed or uncompressed
  228.  
  229. // Function prototypes ****************************************************
  230.  
  231. extern void unpackpcx(FILE *pcx, const char far *source,
  232.                       char far *dest, unsigned int num_bytes);
  233.  
  234. // **** System level graphics functions ***********************************
  235.  
  236. extern void setgraphmode();
  237. extern void settextmode();
  238. extern void wait_vbi();
  239. extern void reporterr(char type, char where[30]);
  240.  
  241. // **** DAC palette functions *********************************************
  242.  
  243. extern void loadpalette(int start, int number, const p_rec palette);
  244. extern void readpalette(int start, int number, p_rec palette);
  245. extern void clrpalette(int start, int number);
  246. extern void fadepalettein(int start, int count, const p_rec palette);
  247. extern void fadepaletteout(int start, int count);
  248.  
  249. // **** Graphics segment redirection **************************************
  250.  
  251. extern void setgraphseg(unsigned newseg);
  252.  
  253. // **** Drawing primitives ************************************************
  254.  
  255. extern void clearscr(int color);
  256. extern void barfill(int tlx, int tly, int brx, int bry, int color);
  257. extern void writepixel(int x, int y, int color);
  258. extern char readpixel(int x, int y);
  259. extern void far *xy_to_ptr(int x, int y);
  260. extern void line(int x0, int y0, int x1, int y1, int color);
  261.  
  262. // **** Image block copying functions *************************************
  263.  
  264. extern void getimage(int x0, int y0, int x1, int y1, char far *buff);
  265. extern void putimage(int x0, int y0, int x1, int y1, char far *buff);
  266. extern void copyimage(int x0, int y0, int x1, int y1, int putx, int puty,
  267.                       void far *src_buf);
  268.  
  269. // **** Image fades and dissolve functions ********************************
  270.  
  271. extern void doSplitVerticalWipe(void far *pic_buf);
  272. extern void doSplitHorizWipe(void far *pic_buf);
  273. extern void doSlideVerticalWipe(void far *pic_buf);
  274. extern void doSlideHorizWipe(void far *pic_buf);
  275. extern void doVerticalDissolve(void far *pic_buf);
  276. extern void doHorizDissolve(void far *pic_buf);
  277. extern void doSparkleDissolve(void far *pic_buf);
  278.  
  279. // ***********************************************************************
  280.  
  281.  
  282.  
  283.